package com.woniuxy.cloud.carl.carlstation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.cloud.carl.carlstation.common.CarlstationResultCode;
import com.woniuxy.cloud.carl.carlstation.infra.mapper.NoticeMapper;
import com.woniuxy.cloud.carl.carlstation.infra.mapper.StaffMapper;
import com.woniuxy.cloud.carl.carlstation.infra.model.Notice;
import com.woniuxy.cloud.carl.carlstation.infra.model.Staff;
import com.woniuxy.cloud.carl.carlstation.service.NoticeService;
import com.woniuxy.cloud.carl.carlstation.service.dto.NoticeServiceDto;
import com.woniuxy.cloud.carl.carlstation.service.param.NoticeServiceParam;
import com.woniuxy.exceptions.MallException;
import com.woniuxy.result.CommonResultCodeEnum;
import com.woniuxy.result.Result;
import com.woniuxy.utils.Assert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2023-07-19
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Resource
    private StaffMapper staffMapper;

    @Override
    public Result addNotice(NoticeServiceParam param) {

        //todo 发布人需根据当前token内绑定的id去查发布人信息
        Notice notice = BeanUtil.copyProperties(param, Notice.class);
        //
        Staff staff = staffMapper.selectById(param.getNoticePublisherId());
        notice.setNoticePublisher(staff.getStaffName());
        //
        notice.setNoticePublishDate(new Date());
        save(notice);
        return Result.success(CommonResultCodeEnum.SUCCESS,"发布成功",null);
    }

    @Override
    public Result delNotice(List<Long> noticeIds) {
        removeByIds(noticeIds);
        return Result.success(CommonResultCodeEnum.SUCCESS,"删除成功",null);
    }

    @Override
    public Result<List<NoticeServiceDto>> getNotice(Long NoticeId, String condition) {
        ArrayList<NoticeServiceDto> noticeServiceDtos = new ArrayList<>();
        if (NoticeId!=null){
            Notice notice = getById(NoticeId);
            Assert.state(notice!=null,"查询条件错误");
            NoticeServiceDto noticeServiceDto = BeanUtil.copyProperties(notice, NoticeServiceDto.class);
            noticeServiceDtos.add(noticeServiceDto);
        }else if (condition!=null&&!condition.equals("")){
            QueryWrapper<Notice> wrapper = new QueryWrapper<>();
            wrapper.like("content",condition).or().like("notice_publisher",condition);
            List<Notice> notices = list(wrapper);
            if (notices==null||notices.size()==0)
                throw new MallException(CommonResultCodeEnum.DATA_NOT_FOUND,"没有此类公告");
            for (Notice notice : notices) {
                NoticeServiceDto noticeServiceDto = BeanUtil.copyProperties(notice, NoticeServiceDto.class);
                noticeServiceDtos.add(noticeServiceDto);
            }
        }else {
            List<Notice> list = list();
            for (Notice notice : list) {
                NoticeServiceDto noticeServiceDto = BeanUtil.copyProperties(notice, NoticeServiceDto.class);
                noticeServiceDtos.add(noticeServiceDto);
            }
        }
        return Result.success(noticeServiceDtos);
    }
}
